﻿@model IPagedList<Class>
@{
    ViewData["Title"] = "Student Groups";
    Affiliation affiliation = ViewBag.Affiliation;
    IReadOnlyList<VerifyCode> codes = ViewBag.Codes;
    bool all = ViewBag.ShowAll;
    int uid = int.Parse(User.GetUserId());
}

<h2 class="mt-2 mb-3">@ViewData["Title"] in @affiliation.Name</h2>

<div asp-show-if="codes != null" class="alert alert-primary" style="max-width:35em">
    <razor asp-show-if="codes.Count == 0">
        Currently you have no student invitation code.
        <a class="alert-link" asp-controller="Tenants" asp-action="CreateVerifyCode" asp-route-returnUrl="@ViewData["RefreshUrl2"]" data-toggle="ajaxWindow">Create one &raquo;</a>
    </razor>
    <razor asp-show-if="codes.Count > 0">
        You can share verify codes to the unverified students.
        They can use this code to verify their student identity.
        <ul class="mb-0 mt-2">
            @foreach (var code in codes)
            {
            <li>
                <b style="font-family:Consolas,monospace">@code.Code</b>
                : Used @code.RedeemCount times,
                created since @code.CreationTime.ToString("d"),
                <a asp-controller="Tenants" asp-action="InvalidateVerifyCode" asp-route-code="@code.Code" asp-route-returnUrl="@ViewData["RefreshUrl2"]" data-toggle="ajaxWindow">invalidate</a>
            </li>
            }
        </ul>
    </razor>
</div>

<div class="table-wrapper">
    <table class="data-table table table-sm table-striped" style="width:auto">
        <thead>
            <tr role="row">
                <th style="min-width:2em" scope="col" class="sortable searchable">ID</th>
                <th style="min-width:10em" scope="col" class="sortable searchable">name</th>
                <th style="min-width:3em" scope="col" class="sortable searchable">count</th>
                <th style="min-width:6em" scope="col" class="sortable">create time</th>
                <th style="min-width:4em" scope="col" class="sortable searchable">creator</th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            @foreach (var stu in Model)
            {
                <tr asp-action="Detail" asp-route-clsid="@stu.Id" role="row" data-toggle="gotoUrl">
                    <td use-a>@stu.Id</td>
                    <td use-a><i asp-show-if="@(!stu.UserId.HasValue)" class="fas fa-tag" title="Shared"></i> @stu.Name</td>
                    <td use-a>@stu.Count</td>
                    <td use-a>@Html.CstTime(stu.CreationTime)</td>
                    <td use-a>@(stu.UserName ?? "N/A")</td>
                    <td><a asp-show-if="@(stu.UserId == null || stu.UserId == uid)" asp-action="Delete" asp-route-clsid="@stu.Id" title="delete this group" data-toggle="ajaxWindow" data-target="deleteGroup"><i class="fas fa-trash-alt"></i></a></td>
                </tr>
            }
        </tbody>
    </table>
</div>

<div class="d-none" id="all-switcher-src">
    <div class="text-right">
        <div class="btn-group btn-group-sm btn-group-toggle text-left" data-toggle="buttons">
            <a asp-route-all="false" class="btn btn-outline-secondary @(all ? "" : "active")">Only Mine</a>
            <a asp-route-all="true" class="btn btn-outline-secondary @(all ? "active" : "")">All</a>
        </div>
    </div>
</div>

<div class="d-none" id="pager-src">
    <pagination class="mb-1" bs-current-page="Model.CurrentPage" bs-total-page="Model.TotalPage" />
</div>

<script>
    $().ready(function () {
        $('.data-table').DataTable({
            "paging": false,
            "searching": false,
            "ordering": true,
            "order": [[0, 'desc']],
            "info": false,
            "autoWidth": true,
            "dom": "<'row'<'#pager.col-sm-12 col-md-6'p><'#all-switcher.col-sm-12 col-md-6'>><'row'<'col-sm-12'tr>><'row'<'col-sm-12 col-md-5'i><'col-sm-12 col-md-7'f>>",
            "language": {
                "searchPlaceholder": "filter table",
                "search": "_INPUT_",
            },
            'aoColumnDefs': [
                { aTargets: ['sortable'], bSortable: true },
                { aTargets: ['searchable'], bSearchable: true },
                { aTargets: ['_all'], bSortable: false, bSearchable: false }
            ],
        });

        $('#all-switcher').html($('#all-switcher-src').html());
        $('#pager').html($('#pager-src').html());
    });
</script>

<p>
    <a asp-action="Create" class="btn btn-primary" data-toggle="ajaxWindow" data-target="createGroup" title="create groups"><i class="fas fa-plus"></i>&nbsp;Create</a>
    <a asp-action="Switch" asp-controller="Tenants" asp-route-returnUrl="@Context.Request.Path.Value" class="btn btn-secondary" title="switch tenant"><i class="fas fa-globe-asia"></i>&nbsp;Switch tenant</a>
</p>
